package com.smart.manager.product.repository;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.Opt;
import cn.hutool.core.util.StrUtil;
import com.smart.common.entities.constant.ArcoTips;
import com.smart.common.entities.core.Page;
import com.smart.common.entities.option.SelectOption;
import com.smart.manager.product.entities.body.RPigGoods;
import com.smart.manager.product.entities.enums.CameraDirection;
import com.smart.manager.product.entities.enums.PigstyKind;
import com.smart.manager.product.entities.enums.ProfileStatus;
import com.smart.manager.product.entities.models.BasicCameraDetail;
import com.smart.manager.product.entities.models.PigGoods;
import com.smart.manager.product.entities.models.query.QBasicCameraDetail;
import com.smart.manager.product.entities.models.query.QPigGoods;
import io.ebean.DB;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


/**
 * 猪仔配置 - 销售管理
 * @author leo
 * @date 2023/09/07
 */
@Service
@Log4j2
@RequiredArgsConstructor
public class PigGoodsRepository {

    private final BasicCameraDetailRepository basicCameraDetailRepository;

    public Page<PigGoods> pageList(RPigGoods.SearchBody search) {
        QPigGoods query = new QPigGoods();
        Opt.ofBlankAble(search.getCode()).ifPresent(x -> query.code.contains(x));
        Opt.ofBlankAble(search.getName()).ifPresent(x -> query.name.contains(x));
        Opt.ofBlankAble(search.getPromotion()).ifPresent(x -> query.promotion.eq(x));
        Opt.ofBlankAble(search.getMode()).ifPresent(x -> query.mode.eq(x));
        Opt.ofBlankAble(search.getStatus()).ifPresent(x -> query.status.eq(x));
        query.order().sort.asc();
        query.setFirstRow(search.firstRow()).setMaxRows(search.maxRows());
        return new Page<PigGoods>().load(query.findPagedList());
    }

    public void add(RPigGoods.EditBody body) {
        QPigGoods query = new QPigGoods();
        PigGoods record = query.code.eq(body.getCode()).setMaxRows(1).findOne();
        Assert.isNull(record, "创建失败:, 商品代码已存在");
        BeanUtil.copyProperties(body, PigGoods.class).save();
    }


    public void update(Long id, RPigGoods.EditBody body) {
        QPigGoods query = new QPigGoods();
        PigGoods record = query.id.eq(id).setMaxRows(1).findOne();
        Assert.notNull(record, ArcoTips.UPDATE_NOT_FOUND);
        BeanUtil.copyProperties(body, record);
        Objects.requireNonNull(record).update();
    }

    public int delByIds(List<Long> ids) {
        List<PigGoods> list = new QPigGoods().id.in(ids).findList();
        Assert.notEmpty(list, ArcoTips.DELETE_NOT_FOUND);
        for (PigGoods item : list) {
                log.warn("判断数据是否能被删除");
        }
        return DB.deleteAll(list);


    }

    /**
     * 获取列表
     * @return {@link List}<{@link PigGoods}>
     */
    public List<PigGoods> getList(){
        List<BasicCameraDetail> individually = new QBasicCameraDetail().cameraDirection.eq(CameraDirection.STORE).setMaxRows(5).findList();
        List<PigGoods> pigGoods = new QPigGoods().status.eq(ProfileStatus.ON).findList();
        pigGoods.forEach(x->{
            if (x.getMode().equals(PigstyKind.ALONE)) {
                Opt.ofEmptyAble(individually).ifPresent(c -> {
                    List<String> list = new ArrayList<>();
                    individually.forEach(o->{
                        String cameraUrl = basicCameraDetailRepository.getCameraUrl(o, false);
                        list.add(cameraUrl);
                    });
                    Collections.shuffle(list);
                    x.setCamera(list);
                });
            }
        });
       return pigGoods;

    }


    /**
     * 获取数据字典下拉选项
     * @return {@code List<SelectOption>}
     */
    public List<SelectOption> getOptions() {
        QPigGoods place = new QPigGoods();
        return place.findStream()
                        .map(x -> new SelectOption().setLabel(x.getName()).setValue(x.getCode()).setDisabled(false))
                        .collect(Collectors.toList());
    }

}






